<!--导入送货计划弹窗-->
<template>
	<!-- 送货计划导入弹窗 -->
	<el-dialog
		title="送货计划导入"
		:visible.sync="importDialogVisible"
		width="50%"
		:before-close="importHandleClose"
		v-dialogDrag
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<importFile
			:moduleName="moduleName"
			@setImportData="getImportData"
			:closeImportDialog="importDialogVisible"
			:tempUrl="tempUrl"
		></importFile>
		<span slot="footer" class="dialog-footer">
			<el-button @click="importHandleClose">取消</el-button>
			<el-button type="primary" @click="saveImportData" :loading="btn_loading">导入</el-button>
		</span>
	</el-dialog>
</template>

<script>
import importFile from "@/components/importFile"
import fileStream from "@/public/fileStream"
import Msg from "@/public/message"
import { maxDate, minDate } from "@/util/format"
export default {
	data () {
		return {
			valitDate: '',
			moduleName: 'deliveryPlan',
			tempUrl: '',
			btn_loading: false
		};
	},
	props: {
		importDialogVisible: {
			type: Boolean
		},
		detailsForm: {
			type: Object
		}
	},
	methods: {
		importHandleClose () {
			this.$emit('closeDialog')
		},
		// 获取导入数据
		getImportData (val) {
			this.importData = val
		},
		// 确认保存数据
		saveImportData () {
			if (Array.isArray(this.importData) && this.importData.length > 0) {
				let dates = this.importData.map(date => {
					return Date.parse(date.requireDate)
				})
				let maxDates = maxDate(dates)
				let minDates = minDate(dates)
				let num = Math.floor((maxDates - minDates) / (24 * 3600 * 1000))
				if (num > 6) {
					Msg.showPopMsg('每次只能做一周内的计划,请重新导入!')
					return
				}
				this.getImportDeliveryPlanData(this.importData)
			} else {
				Msg.showPopMsg('导入数据不能为空!')
			}
		},
		// 获取导入模板
		getContractTemp () {
			fileStream.getPrintTempUrl("deliveryPlan").then(url => {
				this.tempUrl = url
			})
		},
		// 获取导入数据明细
		getImportDeliveryPlanData (planMxs) {
			this.$http.importDeliveryPlan(planMxs).then(({ data }) => {
				if (data.code == 1) {
					data.rows.forEach(row => {
						row.deliveryplanmxs.forEach(item => {
							let amount = 0
							item.deliveryplantms.forEach(el => {
								amount += Number(el.planAmount)
							})
							item.planAmount = amount
							item.confirmAmount = 0
							item.state = 0
						})
						row.type = 0      //0采购方计划
					})
					this.$emit('saveImportData', data.rows)
				} else {
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				Msg.showErrorMsg(err.data.message)
			})
		}
	},
	components: {
		importFile
	},
	beforeMount () {
		this.getContractTemp()
	}
}
</script>
<style lang="less" scoped>
.m-detailsFrom {
	/deep/ .el-form-item {
		margin-bottom: 10px;
		.el-form-item__content {
			.el-textarea.is-disabled {
				.el-textarea__inner {
					color: #333;
				}
			}
		}
	}
	.line-item {
		margin-bottom: 10px;
		label {
			width: 80px;
			text-align: right;
			vertical-align: middle;
			float: left;
			font-size: 14px;
			color: #606266;
			line-height: 30px;
			padding: 0 12px 0 0;
			box-sizing: border-box;
		}
		a {
			line-height: 30px;
			color: #409eff;
		}
	}
}
</style>
